package com.ask.forMe.task;

import com.ask.forMe.model.entity.User;
import com.ask.forMe.service.ItemService;
import com.ask.forMe.service.RecommendService;
import com.ask.forMe.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

import static com.ask.forMe.constant.RedisConstant.*;

@Component
public class RecommendTask {

    @Autowired
    private UserService userService;

    @Autowired
    private RecommendService recommendService;

    @Autowired
    private ItemService itemService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Scheduled(cron = "0 0 1 * * ?")
    public void generateDailyRecommendations() {
        // 1. 获取所有用户
        List<User> userList = userService.list();
        List<Long> userIds = userService.list().stream().map(User::getId).toList();

        // 2. 并行处理每位用户
        userIds.parallelStream().forEach(userId -> {
            // 获取当日的推荐
            List<Long> itemIds = recommendService.getItemRecommendationsForUser(userId, TopN);
            // 将推荐列表(ids)存入set
            recommendService.saveRecommendationIds2Redis(userId, itemIds);
        });
    }
}
